const request = require('request-promise')
const config = require('../config/index')
const log4js = require('log4js')
const schedule = require('node-schedule')
const fs = require('fs-extra')

// 配置log4js
log4js.configure(config.log4js)
const logger = log4js.getLogger('币猎手')

async function getNews () {
  try {
    let json = await request({
      url: 'http://coinhunt.cn/api/user/message?page=1&pag2e_size=10',
      headers: {
        'Host': 'coinhunt.cn',
        'Referer': 'http://coinhunt.cn/',
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,en-US;q=0.8',
        'Cookie': 'coinhunt_session=VyuWM8ZFHBKfDK8RgmGka25HWS8uolJsmjlpS4sp; SERVERID=21d3d2bb0b035c72b88c9c538f79dc0a|1532168549|1532168548',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 8.0; ONEPLUS A3010 Build/OPR1.170623.032; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.7.1321(0x26070030) NetType/WIFI Language/zh_CN'
      }
    })
    await fs.writeFile('1.txt', json)
    await request({
      url: 'https://push.cthai.cn/api/Biquan/save_list',
      method: 'POST',
      body: {json: json},
      json: true
    })
  } catch (e) {
    logger.error(e.message)
    return null
  }
}

/**
 * 每5分钟执行一次查询
 */
schedule.scheduleJob('*/10 * * * * *', async () => {
  await getNews()
})
